package com.hula.onion.book.server.service.impl;


import com.google.common.collect.Maps;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.dao.IAccountInfoDao;
import com.hula.onion.book.server.dao.IPlatformInfoDao;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.service.AccountInfoService;
import com.hula.onion.book.server.vo.AccountInfoVo;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by work on 2018/1/29.
 */
@Service
public class AccountInfoServiceImpl implements AccountInfoService {

    private static final Logger logger = LoggerFactory.getLogger(AccountInfoServiceImpl.class);

    @Autowired
    private IAccountInfoDao accountInfoDao;
    @Autowired
    private IPlatformInfoDao platformInfoDao;

    @Override
    public void createPlatformAccountInfo(int userId, int platformId, int syncType, AccountInfo accountInfo) {
        if (accountInfoDao.haveRecord(userId, platformId)) {//如果已经有记录 直接更新
            boolean flag = accountInfoDao.updatePlatformAccount(userId, platformId, accountInfo);
            if (flag) {
                logger.error("[AccountInfoServiceImpl] createPlatformAccountInfo update fail:userId=" + userId + ",platformId=" + platformId);
            }
        }
        boolean flag = accountInfoDao.createPlatformAccount(userId, platformId, syncType, accountInfo);
        if (!flag) {
            logger.error("[AccountInfoServiceImpl] createPlatformAccountInfo create fail:userId=" + userId + ",platformId=" + platformId);
        }
    }

    @Override
    public void updatePlatformAccountInfo(int userId, int platformId, AccountInfo accountInfo) {
        boolean flag = accountInfoDao.updatePlatformAccount(userId, platformId, accountInfo);
        if (!flag) {
            logger.error("[AccountInfoServiceImpl] createPlatformAccountInfo  fail:userId=" + userId + ",platformId=" + platformId);
        }
    }

    @Override
    public CommonResponse getUserAccount(int userId) {
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
        List<AccountInfoVo> list = accountInfoDao.getUserAccounts(userId);
        BigDecimal totalAccount = BigDecimal.ZERO;
        if (CollectionUtils.isNotEmpty(list)) {
            for (AccountInfoVo accountInfoVo : list) {
                totalAccount = totalAccount.add(accountInfoVo.getAmount());
            }
        }
        map.put("list", list);
        map.put("totalAccount", totalAccount);
        return new CommonResponse(map);
    }

    @Override
    public CommonResponse getUserPlatformAccount(int platformId, int investId) {
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
        map.put("accountInfo", accountInfoDao.getUserPlatformAccount(investId));
        map.put("platformInfo", platformInfoDao.getPlatformById(platformId));
        return new CommonResponse(map);
    }

    @Override
    public CommonResponse userPlatformAccountChart(int userId, int type) {
        List<AccountInfoVo> list = accountInfoDao.getUserAccounts(userId);
        if (CollectionUtils.isNotEmpty(list)) {
            List<Map> list1 = new ArrayList<>();
            BigDecimal totalAmount = BigDecimal.ZERO;
            for (AccountInfoVo accountInfoVo : list) {
                totalAmount = totalAmount.add(accountInfoVo.getAmount());
            }
            for (AccountInfoVo accountInfoVo : list) {
                Map map = Maps.newHashMapWithExpectedSize(3);
                map.put("platformName", accountInfoVo.getName());
                map.put("platformAmount", accountInfoVo.getAmount());
                if (!totalAmount.equals(0)) {
                    map.put("platformRate", accountInfoVo.getAmount()
                            .divide(totalAmount, 2, BigDecimal.ROUND_HALF_UP)
                            .multiply(new BigDecimal(100))+"%");
                } else {
                    map.put("platformRate", 0);
                }
                list1.add(map);
            }
            return new CommonResponse(list1);
        }
        return new CommonResponse();
    }

    @Override
    public void insertOrUpdateAccountInfo(int userId, int platformId, int syncType, AccountInfo accountInfo) {

        AccountInfo info = accountInfoDao.queryByUserIdAndPlatformId(userId, platformId);

        //insert
        if (info == null) {
            createPlatformAccountInfo(userId, platformId, syncType, accountInfo);
            //update
        } else {
            updatePlatformAccountInfo(userId, platformId, accountInfo);
        }
    }

    @Override
    public int delAccountInfo(int id) {
        return accountInfoDao.delUserPlatformAccountById(id);
    }
}
